package com.google.coffeeshop.gui.desktop.statistics;

import static net.sf.dynamicreports.report.builder.DynamicReports.*;

import java.util.Date;

import vn.bang.utility.DateUtil;

import com.google.coffeeshop.business.StatisticsBusiness;
import com.google.coffeeshop.entity.EmployeeDTO;
import com.google.coffeeshop.entity.ProductDTO;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.chart.TimeSeriesChartBuilder;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.style.FontBuilder;
import net.sf.dynamicreports.report.exception.DRException;


public class SaleIncomeDesign {
	public Date beginDate;
	public Date endDate;
	public EmployeeDTO employee = null;
	public ProductDTO product = null;
	public JasperReportBuilder build() throws DRException {
		JasperReportBuilder report = report();
		//init styles
		FontBuilder  boldFont = stl.fontArialBold();

		//init columns
		if(employee == null && product ==null){
			TextColumnBuilder<Long> saleColumn = col.column("Sale", "sale", type.longType());
			TextColumnBuilder<Long> incomeColumn = col.column("Income", "income", type.longType());
			TextColumnBuilder<Date> monthColumn = col.column("Month", "date", type.dateYearToMonthType());		
			//init groups
			//ColumnGroupBuilder productGroup = grp.group(groupColumn);
	
			//init subtotals
			
			//init charts
			TimeSeriesChartBuilder dateChart = cht.timeSeriesChart()
					.setTitle("Sale & Income by month")
					.setTitleFont(boldFont)
					.setTimePeriod(monthColumn)
					.addSerie(
						cht.serie(saleColumn), cht.serie(incomeColumn));
			//configure report
			report
				.setTemplate(Templates.reportTemplate)
				//columns
				.columns(
					saleColumn, incomeColumn, monthColumn)
				//groups
				//.groupBy(productGroup)
				//subtotals
				
				//band components
				.title(
					Templates.createTitleComponent("Sale & Income "+ DateUtil.toString(beginDate) + " - " + DateUtil.toString(endDate)), dateChart)
				.pageFooter(
					Templates.footerComponent)
				.setDataSource(StatisticsBusiness.getSaleBetweenDates(beginDate, endDate, employee, product));
		}
		else if(product !=null) {
			TextColumnBuilder<Long> saleColumn = col.column("Sale", "sale", type.longType());
			TextColumnBuilder<Date> monthColumn = col.column("Month", "date", type.dateYearToMonthType());		
			//init groups
			//ColumnGroupBuilder productGroup = grp.group(groupColumn);
	
			//init subtotals
			
			//init charts
			TimeSeriesChartBuilder dateChart = cht.timeSeriesChart()
					.setTitle("Sale of " + product.getName() + " by month")
					.setTitleFont(boldFont)
					.setTimePeriod(monthColumn)
					.addSerie(
						cht.serie(saleColumn));
			//configure report
			report
				.setTemplate(Templates.reportTemplate)
				//columns
				.columns(
					saleColumn, monthColumn)
				//groups
				//.groupBy(productGroup)
				//subtotals
				
				//band components
				.title(
					Templates.createTitleComponent("Sale of "+ product.getName() + " " + DateUtil.toString(beginDate) + " - " + DateUtil.toString(endDate)), dateChart)
				.pageFooter(
					Templates.footerComponent)
				.setDataSource(StatisticsBusiness.getSaleBetweenDates(beginDate, endDate, employee, product));
		}
		else {
			TextColumnBuilder<Long> saleColumn = col.column("Sale", "sale", type.longType());
			TextColumnBuilder<Date> monthColumn = col.column("Month", "date", type.dateYearToMonthType());		
			//init groups
			//ColumnGroupBuilder productGroup = grp.group(groupColumn);
	
			//init subtotals
			
			//init charts
			TimeSeriesChartBuilder dateChart = cht.timeSeriesChart()
					.setTitle("Sale of " + employee.getFullName() + " by month")
					.setTitleFont(boldFont)
					.setTimePeriod(monthColumn)
					.addSerie(
						cht.serie(saleColumn));
			//configure report
			report
				.setTemplate(Templates.reportTemplate)
				//columns
				.columns(
					saleColumn, monthColumn)
				//groups
				//.groupBy(productGroup)
				//subtotals
				
				//band components
				.title(
					Templates.createTitleComponent("Sale of "+ employee.getFullName() + " " + DateUtil.toString(beginDate) + " - " + DateUtil.toString(endDate)), dateChart)
				.pageFooter(
					Templates.footerComponent)
				.setDataSource(StatisticsBusiness.getSaleBetweenDates(beginDate, endDate, employee, product));
		}
	
				return report;
	}

	
}